package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thrd.taiexhibition.bean.BeanCityIndustryDetailCompany;
import com.thrd.taiexhibition.bean.BeanCityIndustryDetailCompanyData;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;

import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/25
 */
public class FocusIndustryCompanyInfoView extends FrameLayout {

    private List<BeanCityIndustryDetailCompany> beanCityIndustryDetailCompanieList;

    public FocusIndustryCompanyInfoView(@NonNull Context context) {
        super(context);
    }

    public FocusIndustryCompanyInfoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusIndustryCompanyInfoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private Handler mHandler=new Handler(Looper.getMainLooper());

    private BeanCityIndustryDetailCompanyData mBeanCityIndustryDetailCompany;
    public  void setTouchData(BeanCityIndustryDetailCompanyData mBeanCityIndustryDetailCompanyData){
        this.mBeanCityIndustryDetailCompany=mBeanCityIndustryDetailCompanyData;
        beanCityIndustryDetailCompanieList = mBeanCityIndustryDetailCompany.getCityIndustryDetailCompanyList();
        addImageView(beanCityIndustryDetailCompanieList);

       // setControllerListener(this, Constant.IMAGE+mBeanCityIndustryDetailCompany.getLocationImg(),getContext());

    }

    private void addImageView(List<BeanCityIndustryDetailCompany>  beanCityIndustryDetailsList) {
        removeAllViews();
        if(beanCityIndustryDetailsList!=null){
            for (BeanCityIndustryDetailCompany mBeanCityIndustryDetail:beanCityIndustryDetailsList){
                FrescoImage mFrescoImage=new FrescoImage(getContext());
                FrameLayout.LayoutParams mLayoutParams=new FrameLayout.LayoutParams(mBeanCityIndustryDetail.getWidth(),mBeanCityIndustryDetail.getLength());
                mLayoutParams.topMargin=mBeanCityIndustryDetail.getyAxis();
                mLayoutParams.leftMargin=mBeanCityIndustryDetail.getxAxis();
                mFrescoImage.setLayoutParams(mLayoutParams);
                mFrescoImage.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(onTouchImageListener!=null){
                            onTouchImageListener.onTouchImageListener(mBeanCityIndustryDetail);
                        }

                    }
                });
                FrescoUtils.getInstance().setPrefixProgressLoad(getContext(),mFrescoImage,mBeanCityIndustryDetail.getIndustryCompanyIcon());
                addView(mFrescoImage);
            }
        }



    }



//    private int touchX;
//    private int touchY;
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                touchX = (int) event.getX();
//                touchY = (int) event.getY();
//                //   Log.e(TAG,"touchX="+touchX);
//                break;
//
//            case MotionEvent.ACTION_UP:
//                int x = (int) event.getX();
//                int y = (int) event.getY();
//                LogUtil.e("xyxy","x="+x+"y="+y);
//                if ((Math.abs(x - touchX) < 30) && (Math.abs(y - touchY) < 30)&&onTouchImageListener!=null) {
//
//                    for (BeanCityIndustryDetailCompany mBeanCityIndustryDetailCompany :beanCityIndustryDetailCompanieList){
//                        if(isArea(x,y,mBeanCityIndustryDetailCompany)){
//                            onTouchImageListener.onTouchImageListener(mBeanCityIndustryDetailCompany);
//                            break;
//                        }
//                    }
//
//
//                }
//
//                break;
//
//
//            default:
//                break;
//        }
//
//        return true;
//    }
//
//    private boolean isArea(float x,float y,BeanCityIndustryDetailCompany mBeanCityIndustryDetailCompany){
//        int width=mBeanCityIndustryDetailCompany.getWidth()/2;
//        int hight=mBeanCityIndustryDetailCompany.getLength()/2;
//        if(Math.abs(x- (mBeanCityIndustryDetailCompany.getxAxis()+width))<width&&Math.abs(y- (mBeanCityIndustryDetailCompany.getyAxis()+hight))<hight){
//            return true;
//        }
//        return false;
//    }


//
//        /**通过imageWidth 的宽度，自动适应高度
//         * @param simpleDraweeView view
//         * @param imagePath  Uri
//         * @param imageWidth width
//         * @param mContext  context
//         */
//        public  void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, Context mContext) {
//            final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
//
//
//            ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
//                @Override
//                public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
//                    if (imageInfo == null) {
//                        return;
//                    }
//                    int height = imageInfo.getHeight();
//                    int width = imageInfo.getWidth();
//                    layoutParams.width = width;//imageWidth;
//                    layoutParams.height = height;//(int) ((float) (imageWidth * height) / (float) width);
//                    simpleDraweeView.setLayoutParams(layoutParams);
//                }
//
//                @Override
//                public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
//                    Log.d("TAG", "Intermediate image received");
//                }
//
//                @Override
//                public void onFailure(String id, Throwable throwable) {
//                    throwable.printStackTrace();
//                }
//            };
//
//        DraweeController controller = Fresco.newDraweeControllerBuilder()
//                .setControllerListener(controllerListener)
//                .setUri(Uri.parse(imagePath))
//                .build();
//        simpleDraweeView.setController(controller);
//    }

//    private   void setControllerListener(final ImageView mImageView, String imagePath, Context mContext) {
//        OkHttpUtils.get().url(imagePath).build().execute(new BitmapCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                LogUtil.e("Image","onError");
//            }
//
//            @Override
//            public void onResponse(Bitmap response, int id) {
//                LogUtil.e("Image","onError");
//                mImageView.setImageBitmap(response);
//            }
//
//            @Override
//            public Bitmap onJson(String json) throws Exception {
//                return null;
//            }
//
//
//        });
//    }

    private OnTouchImageListener onTouchImageListener;

    public OnTouchImageListener getOnTouchImageListener() {
        return onTouchImageListener;
    }

    public void setOnTouchImageListener(OnTouchImageListener onTouchImageListener) {
        this.onTouchImageListener = onTouchImageListener;
    }

    public interface OnTouchImageListener{

        void onTouchImageListener(BeanCityIndustryDetailCompany mBeanCityIndustryDetail);
    }


}
